package com.example.system.controller;

import com.example.system.common.Result;
import com.example.system.entity.Health;
import com.example.system.entity.Params;
import com.example.system.entity.Care;
import com.example.system.exception.CustomException;
import com.example.system.service.impl.CareServiceImpl;
import com.example.system.service.impl.UserServiceImpl;
import com.github.pagehelper.PageInfo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/care")
@MapperScan("com.*.mapper")
public class CareController {
    @Autowired
    private CareServiceImpl careService;

    @Autowired
    private UserServiceImpl userService;

    //增删改查
    //新增 & 修改
    @PostMapping("/add")
    public Result addCareByCare(@RequestBody Care care) {
        if (care.getUserId() == null) {
            return Result.error("user_id不能为空");
        }
        if (careService.findCareByUserID(care.getUserId()) == null) {
            return Result.error("当前用户不存在");
        }
        if (care.getCareId() == null) {
            if (care.getFinish() == null) {
                care.setFinish(0);
            }
            try {
                careService.addCareByCare(care);
            } catch (CustomException e) {
                return Result.error(e.getMsg());
            }
        } else {
            try {
                careService.updateCareByCare(care);
            } catch (CustomException e) {
                return Result.error(e.getMsg());
            }
        }
        return Result.success();
    }

    // 根据护理ID删除护理信息
    @DeleteMapping("/delete/{careId}")
    public Result deleteCareByCareId(@PathVariable Integer careId) {
        Integer deleteRes = careService.deleteCareByCareId(careId);
        return Result.success(deleteRes);
    }


    // 根据护理ID查护理信息
    @GetMapping("/search/self/{careId}")
    public Result findCareByCareID(@PathVariable Integer careId) {
        Care curCare = careService.findCareByCareID(careId);
        if (curCare == null) {
            return Result.error("当前用户不存在");
        }
        return Result.success(curCare);
    }

    // 根据用户ID查护理信息
    @GetMapping("/search/{userId}")
    public Result findByUserId(@PathVariable Integer userId) {
        List<Care> careList = careService.findCareByUserID(userId);
        if (careList == null || careList.isEmpty()) {
            return Result.error("当前用户尚无护理记录");
        }
        return Result.success(careList);
    }

}
